#include "keyboard.h"


int mW_keyboard_init ()
{
	if (install_keyboard() != 0)
		return 0;
	else
		return 1;
}
	


char mW_keyboard_get_char ()
{
	return (char) (readkey() & 0xFF);
}



/*
 * Returns a string of keyboard input, up to 255 characters
 *   string is computed when user hits enter
 * Also shows user what he's entering, centered on the screen
 * REMEMBER TO FREE RESULT WHEN DONE
 */
char * mW_keyboard_input_string (int max_length)
{
	
	int string_i = 0;
	unsigned char read_char;
	
	if (max_length > 255)
		return NULL;
	
	// where we will store our string we are working on
	char * string_chars;
	string_chars = (char *) malloc(sizeof(char) * max_length + 1);
		if (string_chars == NULL)
			return NULL;
	memset(string_chars, 0, max_length + 1);
	
	// we set mW_center_text equal to result_string_chars so they will automatically be
	//   drawn to the center of the screen
	mW_centered_text = string_chars;
	
	while (1)
	{	                             
		
		read_char = mW_keyboard_get_char();
		
		// backspace
		if (read_char == 8)
		{
			if (string_i > 0)
				string_chars[--string_i] = 0;
		}
		// return
		else if (read_char == 13)
		{
			break;
		}
		else if (string_i < max_length)
		{
			string_chars[string_i++] = read_char;
		}
		
	}
	
	mW_centered_text = NULL;
	return string_chars;
	
}
			
